package net.togogo.egouweb1238.core.controller;

import java.io.StringWriter;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import net.togogo.egouweb1238.common.utils.Constants;
import net.togogo.egouweb1238.common.web.session.SessionProvider;
import net.togogo.egouweb1238.core.domain.cart.BuyCart;
import net.togogo.egouweb1238.core.domain.cart.BuyItem;
import net.togogo.egouweb1238.core.domain.order.Order;
import net.togogo.egouweb1238.core.domain.product.Sku;
import net.togogo.egouweb1238.core.domain.user.Addr;
import net.togogo.egouweb1238.core.domain.user.Buyer;
import net.togogo.egouweb1238.core.query.user.AddrQuery;
import net.togogo.egouweb1238.core.service.order.IOrderService;
import net.togogo.egouweb1238.core.service.product.ISkuService;
import net.togogo.egouweb1238.core.service.user.IAddrService;

/**
 * 购物车 
 * @author stone
 *
 */
@Controller
public class CartController {
	@Resource
	private ISkuService skuService;
	@Resource
	private IAddrService addrService;
	@Resource
	private SessionProvider sessionProvider;
	@Resource
	private IOrderService orderService;

	/**
	 * 购买按钮，将商品加到购物车中
	 * @param skuId
	 * @param amount
	 * @param buyLimit
	 * @param productId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * 
	 */
	@RequestMapping(value = "/shopping/buyCart")
	public String buyCart(Integer skuId, Integer amount, Integer buyLimit, Integer productId,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		//第一步:Sku
		//springmvc 
		ObjectMapper om = new ObjectMapper();
		om.setSerializationInclusion(Inclusion.NON_NULL);

		//声明
		BuyCart buyCart = null;
		//判断Cookie是否有购物车  

		//JESSIONID
		//buyCart_cookie
		//  
		Cookie[] cookies = request.getCookies();
		if (null != cookies && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (Constants.BUYCART_COOKIE.equals(c.getName())) {
					//如果有了  就使用此购物车
					String value = c.getValue();//
					//
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
			}
		}
		//如果有了  就使用此购物车
		//如果没有 创建购物车

		if (null == buyCart) {
			//购物车   //最后一款
			buyCart = new BuyCart();
		}
		//
		if (null != skuId) {
			Sku sku = new Sku();
			sku.setId(skuId);
			//限制
			if (null != buyLimit) {
				sku.setSkuUpperLimit(buyLimit);
			}
			//创建购物项
			BuyItem buyItem = new BuyItem();

			buyItem.setSku(sku);
			//数量  1 2 3  -1
			buyItem.setAmount(amount);
			//添加购物项
			buyCart.addItem(buyItem);
			//最后一款商品的ID
			if (null != productId) {
				buyCart.setProductId(productId);
			}

			//流
			StringWriter str = new StringWriter();
			//对象转Json  写的过程     Json是字符串流
			try {
				om.writeValue(str, buyCart);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//购物车装进Cookie中   对象转Json
			Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, str.toString());
			//关闭浏览器 也要有Cookie
			//默认是 -1  关闭浏览器 就没了
			//消灭  0  马上就没有
			//expiry 秒
			cookie.setMaxAge(60 * 60 * 24);
			//路径
			///shopping/buyCart.thtml
			//默认  /shopping
			//  /shopping
			// /buyer/*.thtml
			cookie.setPath("/");
			//发送
			response.addCookie(cookie);
		}
		//装购物车装满
		List<BuyItem> items = buyCart.getItems();
		for (BuyItem item : items) {
			Sku s = skuService.getSkuById(item.getSku().getId());
			item.setSku(s);
			//小计
		}
		//小计

		model.addAttribute("buyCart", buyCart);

		return "product/cart";
	}

	//清空购物车
	@RequestMapping(value = "/shopping/clearCart")
	public String clearCart(HttpServletRequest request, HttpServletResponse response) {

		Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);

		return "redirect:/shopping/buyCart.thtml";
	}

	//删除一个购物项
	@RequestMapping(value = "/shopping/deleteItem.thtml")
	public String deleteItem(HttpServletRequest request, Integer skuId, HttpServletResponse response) {
		//springmvc 
		ObjectMapper om = new ObjectMapper();
		om.setSerializationInclusion(Inclusion.NON_NULL);

		//声明
		BuyCart buyCart = null;
		//判断Cookie是否有购物车  

		//JESSIONID
		//buyCart_cookie
		//  
		Cookie[] cookies = request.getCookies();
		if (null != cookies && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (Constants.BUYCART_COOKIE.equals(c.getName())) {
					//如果有了  就使用此购物车
					String value = c.getValue();//
					//
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
			}
		}
		if (null != buyCart) {
			Sku sku = new Sku();
			sku.setId(skuId);
			//创建购物项
			BuyItem buyItem = new BuyItem();
			buyItem.setSku(sku);

			buyCart.deleteItem(buyItem);

			//流
			StringWriter str = new StringWriter();
			//对象转Json  写的过程     Json是字符串流
			try {
				om.writeValue(str, buyCart);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//购物车装进Cookie中   对象转Json
			Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, str.toString());
			//关闭浏览器 也要有Cookie
			//默认是 -1  关闭浏览器 就没了
			//消灭  0  马上就没有
			//expiry 秒
			cookie.setMaxAge(60 * 60 * 24);
			//路径
			///shopping/buyCart.thtml
			//默认  /shopping
			//  /shopping
			// /buyer/*.thtml
			cookie.setPath("/");
			//发送
			response.addCookie(cookie);

		}
		return "redirect:/shopping/buyCart.thtml";
	}

	//结算
	@RequestMapping(value = "/buyer/closeBuy.thtml")
	public String closeBuy(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		//springmvc 
		ObjectMapper om = new ObjectMapper();
		om.setSerializationInclusion(Inclusion.NON_NULL);

		//声明
		BuyCart buyCart = null;
		//判断Cookie是否有购物车  

		//JESSIONID
		//buyCart_cookie
		//  
		Cookie[] cookies = request.getCookies();
		if (null != cookies && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (Constants.BUYCART_COOKIE.equals(c.getName())) {
					//如果有了  就使用此购物车
					String value = c.getValue();//
					//
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
			}
		}
		//判断购物车中是否有商品
		if (null != buyCart) {
			//判断购物车中的商品是否还有库存
			List<BuyItem> items = buyCart.getItems();
			if (null != items && items.size() > 0) {
				//购物车的商品项
				Integer i = items.size();

				//判断购物车中的商品是否还有库存
				for (BuyItem it : items) {
					//
					Sku sku = skuService.getSkuById(it.getSku().getId());
					//判断库存
					if (sku.getStockInventory() < it.getAmount()) {
						//删除此商品
						buyCart.deleteItem(it);
					}
				}
				//清理后商品项个数   l=0
				Integer l = items.size();
				//判断清理前后
				if (i > l) {
					//修改Cookie中的购物车数据
					//流
					StringWriter str = new StringWriter();
					//对象转Json  写的过程     Json是字符串流
					try {
						om.writeValue(str, buyCart);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//购物车装进Cookie中   对象转Json
					Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, str.toString());
					//关闭浏览器 也要有Cookie
					//默认是 -1  关闭浏览器 就没了
					//消灭  0  马上就没有
					//expiry 秒
					cookie.setMaxAge(60 * 60 * 24);
					//路径
					///shopping/buyCart.thtml
					//默认  /shopping
					//  /shopping
					// /buyer/*.thtml
					cookie.setPath("/");
					//发送
					response.addCookie(cookie);

					return "redirect:/shopping/buyCart.thtml";
				} else {
					//收货地址加载
					Buyer buyer = (Buyer) sessionProvider.getAttribute(request, response, Constants.BUYER_SESSION);
					AddrQuery addrQuery = new AddrQuery();
					addrQuery.setBuyerId(buyer.getUsername());
					//默认是1
					addrQuery.setIsDef(1);

					List<Addr> addrs = addrService.getAddrList(addrQuery);

					model.addAttribute("addr", addrs.get(0));
					//装购物车装满
					List<BuyItem> its = buyCart.getItems();
					for (BuyItem item : its) {
						Sku s = skuService.getSkuById(item.getSku().getId());
						item.setSku(s);
						//小计
					}
					//小计

					model.addAttribute("buyCart", buyCart);

					//正常
					return "product/productOrder";
				}

			} else {
				return "redirect:/shopping/buyCart.thtml";
			}
		} else {
			return "redirect:/shopping/buyCart.thtml";
		}
	}

	//提交订单
	@RequestMapping(value = "/buyer/confirmOrder")
	public String confirmOrder(Order order, HttpServletRequest request, HttpServletResponse response) {
		//1:接收前台传四个参数

		//springmvc 
		ObjectMapper om = new ObjectMapper();
		om.setSerializationInclusion(Inclusion.NON_NULL);

		//声明
		BuyCart buyCart = null;
		//判断Cookie是否有购物车  

		//JESSIONID
		//buyCart_cookie
		//  
		Cookie[] cookies = request.getCookies();
		if (null != cookies && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (Constants.BUYCART_COOKIE.equals(c.getName())) {
					//如果有了  就使用此购物车
					String value = c.getValue();//
					//
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
			}
		}

		//装购物车装满
		List<BuyItem> its = buyCart.getItems();
		for (BuyItem item : its) {
			Sku s = skuService.getSkuById(item.getSku().getId());
			item.setSku(s);
			//小计
		}

		Buyer buyer = (Buyer) sessionProvider.getAttribute(request, response, Constants.BUYER_SESSION);
		//用户Id
		order.setBuyerId(buyer.getUsername());
		//保存订单   订单详情  二张表
		orderService.addOrder(order, buyCart);
		//清空购物车
		Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, null);
		cookie.setPath("/");
		cookie.setMaxAge(0);
		response.addCookie(cookie);

		return "product/confirmOrder";
	}
}
